<template>
  <div class="login-container">
    <div class="background"></div>
    <div class="login-form">
      <div class="title">
        <a-icon type="icon-Union4" class="icon"></a-icon>
        <span> 简历管理系统</span>
      </div>
      <div class="form-wrap">
        <a-form
          :model="loginForm"
          name="basic"
          :label-col="{ span: 6 }"
          :wrapper-col="{ span: 20 }"
          autocomplete="off"
          @submit="handleSubmit"
        >
          <a-form-item
            label="用户名"
            name="userAccount"
            :rules="[{ required: true, message: '请输入用户名!' }]"
          >
            <a-input v-model:value="loginForm.userAccount" />
          </a-form-item>
          <a-form-item label="密码" name="userPassword" :rules="[{ required: true }]">
            <a-input-password v-model:value="loginForm.userPassword" />
          </a-form-item>
          <a-form-item
            label="确认密码"
            name="userPassword"
            :rules="[{ required: true }]"
            @change="handleSecondPasswordChange"
          >
            <a-input-password v-model:value="loginForm.checkPassword" />
          </a-form-item>
        </a-form>
        <div class="button-group">
          <a-button class="login-button" type="primary" @click="handleRegister">注册</a-button>
          <a-button class="register-button" @click="router.push('/login')">返回</a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import server from '@/api'
import { reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { message } from 'ant-design-vue'

const store = useUserStore()
const router = useRouter()

const loginForm = reactive({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

const handleRegister = async () => {
  try {
    if (loginForm.userAccount === '') {
      message.warning('请输入用户名')
      return
    }
    if (loginForm.userPassword === '') {
      message.warning('请输入密码')
      return
    }
    if (loginForm.checkPassword === '') {
      message.warning('请确认密码')
      return
    }
    if (loginForm.userPassword !== loginForm.checkPassword) {
      message.warning('两次密码不一致')
      return
    }
    if (loginForm.userAccount.length < 4) {
      message.warning('用户名长度不能小于4')
      return
    }

    if (loginForm.userPassword.length < 8) {
      message.warning('密码长度不能小于8')
      return
    }

    const res = await server.register({
      userAccount: loginForm.userAccount,
      userPassword: loginForm.userPassword,
      checkPassword: loginForm.checkPassword,
    })
    if (res.data.code === 0) {
      message.success('注册成功')
      router.push('/login')
    } else {
      message.warning(res.data.message)
    }
  } catch (error) {
    message.warning(error)
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  width: 100%;
  height: 100vh;
}

.background {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-position: center;
  background-size: cover;
  background-color: #011427;
  background-image: url('@/assets/images/login-bg.jpg');
}

.login-form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 25%;
  right: 20%;
  width: 350px;
  min-width: 300px;
  height: 400px;
  border-radius: 8px;
  background-color: #e8e8e8;
  box-shadow: 0 0 10px rgb(0 0 0 / 10%);
}

.form-wrap {
  width: 90%;
  padding: 10px;

  .button-group {
    margin-left: 80px;

    .login-button {
      width: 100px;
    }

    .register-button {
      width: 100px;
      margin-left: 35px;
    }
  }
}

.title {
  height: 50px;
  font-size: 16px;
  text-align: center;
  line-height: 50px;

  .icon {
    margin-right: 10px;
    font-size: 16px;
  }
}

a-form-item {
  width: 100%;
  margin-bottom: 20px;
}
</style>
